﻿using System;
using System.Collections.Generic;
using LayerEntity.Sistema;

namespace System2012.Sistema.Ubigeos
{
    public partial class BuscarUbigeo : DevExpress.XtraEditors.XtraForm
    {
        #region Variables
        string desciption = string.Empty;
        List<LayerEntity.Sistema.EUbigeo> listUigeos = new List<LayerEntity.Sistema.EUbigeo>();
        #endregion

        #region Propiedades
        public EUbigeo UbigeoSelected { get; set; }
        public Boolean StateSelection { get; set; }
        #endregion

        #region Query Ubigeos
        private void LoadUbigeos()
        {
            if (this.gridView1.RowCount > 0)
            {
                this.progressBarControl1.Text = "Quering:";
                this.progressBarControl1.EditValue = 20;
                this.listUigeos = SystemUtils.RecordManagerCommonList.GetUbigeoByDescripcion(this.textEdit1.EditValue.ToString());
                this.gridControl1.DataSource = this.listUigeos;
                this.gridControl1.RefreshDataSource();
                this.progressBarControl1.Text = "Complete:";
                this.progressBarControl1.EditValue = 100;
            }
            else
            {
                if (!this.BGWLoadingUbigeos.IsBusy)
                {
                    //this.listUigeos = new List<LayerEntity.Sistema.EUbigeo>();
                    this.BGWLoadingUbigeos.RunWorkerAsync(this.textEdit1.EditValue);
                }
                //else
                //{
                //    SystemUtils.ShowMessaje("Ya se esta cargando la Informacion de Ubigeos", MessageBoxButtons.OK, MessageBoxIcon.Error);
                //}
            }
        }

        void Seleccionar()
        {
            if (this.gridView1.RowCount > 0)
            {
                this.UbigeoSelected = (EUbigeo)this.gridView1.GetFocusedRow();

                this.StateSelection = true;
                this.Close();
            }
            else
            {
                this.StateSelection = false;
            }
        }

        #endregion

        public BuscarUbigeo()
        {
            this.InitializeComponent();

            this.textEdit1.EditValue = string.Empty;
        }

        private void BuscarUbigeo_Load(object sender, EventArgs e)
        {
            this.LoadUbigeos();
        }
        
        private void BgwLoadingUbigeosDoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
        {
            try
            {
                this.BGWLoadingUbigeos.ReportProgress(30);
                this.listUigeos = SystemUtils.RecordManagerCommonList.GetUbigeoByDescripcion(e.Argument.ToString());
                this.BGWLoadingUbigeos.ReportProgress(60);
                this.BGWLoadingUbigeos.ReportProgress(100);
                e.Result = 1;
            }
            catch (Exception)
            {
                e.Result = 0;
            }
        }

        private void BgwLoadingUbigeosProgressChanged(object sender, System.ComponentModel.ProgressChangedEventArgs e)
        {
            this.progressBarControl1.EditValue = e.ProgressPercentage;
            this.progressBarControl1.Text = "Loading:";
            //this.progressBarControl1.Text = string.Format("{0} %", this.progressBarControl1.EditValue.ToString());
        }

        private void BgwLoadingUbigeosRunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
        {
            this.gridControl1.DataSource = this.listUigeos;
            this.gridControl1.RefreshDataSource();
            this.progressBarControl1.EditValue = 100;
            this.progressBarControl1.Text = "Complete:";
        }

        private void SeleccionarUbigeo(object sender, EventArgs e)
        {
            this.Seleccionar();
        }

        private void CambiandoTexto(object sender, EventArgs e)
        {
            this.LoadUbigeos();
        }
    }
}